package com.mosai.alignpro.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.mosai.alignpro.DiagnosisListActivity;
import com.mosai.alignpro.R;
import com.mosai.alignpro.entity.DoctorInfo;
import com.mosai.alignpro.entity.ImageInfo;

import java.util.Collections;
import java.util.List;

/**
 * 诊断历史列表适配器
 * Created by Rays on 2020/3/9.
 */
public final class DiagnosisHistoryAdapter extends RecyclerView.Adapter<DiagnosisHistoryAdapter.ViewHolder>
        implements View.OnClickListener {

    private final Context context;
    private final RequestOptions doctorOptions;
    private List<ImageInfo> data;
    private final DoctorInfo doctorInfo;
    private final boolean isCanConfirm;
    private final Dialog dialog;

    public DiagnosisHistoryAdapter(Context context, List<ImageInfo> data, DoctorInfo doctorInfo, boolean isCanConfirm, Dialog dialog) {
        super();
        this.context = context;
        this.doctorInfo = doctorInfo;
        this.isCanConfirm = isCanConfirm;
        this.dialog = dialog;
        setData(data);
        int doctorRadius = context.getResources().getDimensionPixelSize(R.dimen.doctor_avatar_size) >> 1;
        doctorOptions = new RequestOptions()
                .transform(new CenterCrop(), new RoundedCorners(doctorRadius));
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = View.inflate(context, R.layout.item_diagnosis_history, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final ImageInfo item = data.get(position);

        Glide.with(context)
                .load(item.getDoctorInfo().getAvatar())
                .placeholder(R.drawable.default_avatar)
                .error(R.drawable.default_avatar)
                .apply(doctorOptions)
                .into(holder.ivDoctorAvatar);
        holder.tvDoctorName.setText(item.getDoctorInfo().getName());
        holder.tvDate.setText(item.getHistoryCreationTime());

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        DiagnosisListActivity.startHistory((Activity) context, data.get(position), doctorInfo, isCanConfirm);
        dialog.dismiss();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public List<ImageInfo> getData() {
        return data;
    }

    public void setData(List<ImageInfo> data) {
        this.data = data == null ? Collections.emptyList() : data;
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivDoctorAvatar;
        TextView tvDoctorName, tvDate;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDoctorName = itemView.findViewById(R.id.tvDoctorName);
            tvDate = itemView.findViewById(R.id.tvDate);
            ivDoctorAvatar = itemView.findViewById(R.id.ivDoctorAvatar);
        }
    }
}
